package com.test.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.test.dao.IEmployeeDao;
import com.test.enity.Employee;
import com.test.utils.DBUtil;

public class EmployeeDaoImpl implements IEmployeeDao {

	
	// 根据id判断员工是否存在
	public boolean isExist(int eid) {
		return queryEmployeeByEid(eid) == null ? false : true;
	}

	// 增加员工
	public boolean addEmployee(Employee employee) {

		String sql = "insert into employee(name,sex,age,dapartment,type,year) values (?,?,?,?,?,?)";
		Object params[] = { employee.getName(), employee.getSex(), employee.getAge(), employee.getDapartment(),
				employee.getType(), employee.getYear() };
		return DBUtil.executeUpdate(sql, params);
	}

	// 删除员工
	public boolean deleteEmployeeByEid(int eid) {

		String sql = "delete from employee where id=?";
		Object params[] = { eid };
		return DBUtil.executeUpdate(sql, params);
	}

	// 根据id修改员工
	public boolean updateEmployeeByEid(int eid, Employee employee) {

		String sql = "update employee set name=?,sex=?,age=?,dapartment=?,type=?,year=? where id=?";
		Object params[] = { employee.getName(), employee.getSex(), employee.getAge(), employee.getDapartment(),
				employee.getType(), employee.getYear(), eid };
		return DBUtil.executeUpdate(sql, params);
	}

	// 根据id查询员工
	public Employee queryEmployeeByEid(int eid) {

		Employee employee = null;
		ResultSet rs = null;

		try {

			String sql = "select * from employee where id=?";
			Object params[] = { eid };
			rs = DBUtil.executeQuery(sql, params);
			if (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String sex = rs.getString("sex");
				int age = rs.getInt("age");
				String dapartment = rs.getString("dapartment");
				String type = rs.getString("type");
				int year = rs.getInt("year");
				employee = new Employee(id, name, sex, age, dapartment, type, year);
			}
			return employee;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (DBUtil.pstmt != null)
					DBUtil.pstmt.close();
				if (DBUtil.connection != null)
					DBUtil.connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	// 查询所有员工
	public List<Employee> queryAllEmployee() {

		List<Employee> employees = new ArrayList<>();
		Employee employee = null;
		ResultSet rs = null;

		try {

			String sql = "select * from employee ";
			rs = DBUtil.executeQuery(sql, null);

			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String sex = rs.getString("sex");
				int age = rs.getInt("age");
				String dapartment = rs.getString("dapartment");
				String type = rs.getString("type");
				int year = rs.getInt("year");
				employee = new Employee(id, name, sex, age, dapartment, type, year);
				employees.add(employee);
			}
			return employees;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (DBUtil.pstmt != null)
					DBUtil.pstmt.close();
				if (DBUtil.connection != null)
					DBUtil.connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
